package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Tactics;
import com.battle.pojo.entity.TacticsType;
import com.battle.pojo.entity.TacticsUser;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.tactics.*;
import com.battle.pojo.param.tacticsUser.CreateTacticsUserParam;
import com.battle.pojo.vo.tactics.*;
import com.battle.pojo.dto.tactics.*;
import com.battle.service.repository.in.TacticsRepository;
import com.battle.service.repository.in.TacticsTypeRepository;
import com.battle.service.repository.in.TacticsUserRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.TacticsService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：战术服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TacticsServiceImpl implements TacticsService {

    private final TacticsRepository tacticsRepository;
    private final UserRepository userRepository;
    private final TacticsUserRepository tacticsUserRepository;
    private final TacticsTypeRepository typeRepository;

    public TacticsServiceImpl(TacticsRepository tacticsRepository, UserRepository userRepository, TacticsUserRepository tacticsUserRepository, TacticsTypeRepository typeRepository) {
        this.tacticsRepository = tacticsRepository;
        this.userRepository = userRepository;
        this.tacticsUserRepository = tacticsUserRepository;
        this.typeRepository = typeRepository;
    }


    /**
     * 创建战术
     *
     * @param param
     */
    @Override
    public void createTactics(CreateTacticsParam param) {
        Tactics tactics = new Tactics();
        BeanUtils.copyProperties(param, tactics);
        Long id = IdGenerator.SNOW.generate();
        tactics.setId(id);
        tactics.setStudyNumber(0);
        tacticsRepository.create(tactics);
    }

    /**
     * 修改战术
     *
     * @param param
     */
    @Override
    public void editTactics(EditTacticsParam param) {
        Long id = param.getId();
        Tactics tactics = tacticsRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, tactics);
        tacticsRepository.edit(tactics);
    }

    /**
     * 移除战术
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTactics(Long id) {
        tacticsRepository.removeById(id);

        TacticsUser tacticsUser = new TacticsUser();
        tacticsUser.setTacticsId(id);

        List<TacticsUser> deleteList = tacticsUserRepository.getListByEntity(tacticsUser);
        if (CollectionUtil.isNotEmpty(deleteList)){
            List<Long> ids = deleteList.stream().map(TacticsUser::getId).collect(Collectors.toList());
            tacticsUserRepository.removeByIds(ids);
        }
    }

    /**
     * 启动/禁用战术
     *
     * @param id
     */
    @Override
    public void enabledTactics(Long id) {
        Tactics tactics = tacticsRepository.getByIdCheck(id);
        tactics.setIsDisable(!tactics.getIsDisable());
        tacticsRepository.edit(tactics);
    }

    /**
     * 获取战术详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTacticsVo getTactics(Long id) {
        Tactics tactics = tacticsRepository.getByIdCheck(id);
        GetTacticsVo vo = new GetTacticsVo();
        BeanUtils.copyProperties(tactics, vo);

        User user = userRepository.getById(tactics.getUpdater());
        if (user!=null){
            vo.setUpdateName(user.getName());
            vo.setUpdateTime(tactics.getUpdateTime());
        }

        Tactics type = tacticsRepository.getById(tactics.getTacticsTypeId());
        if (type!=null){
            vo.setTacticsTypeName(type.getTacticsName());
        }
        return vo;
    }

    /**
     * 创建战术学习
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTacticsUser(CreateTacticsUserParam param) {
        TacticsUser tacticsUser = new TacticsUser();
        BeanUtils.copyProperties(param, tacticsUser);
        TacticsUser byEntity = tacticsUserRepository.getByEntity(tacticsUser);
        if (byEntity != null) {
            return;
        }

        Long id = IdGenerator.SNOW.generate();
        BeanUtils.copyProperties(param, tacticsUser);
        tacticsUser.setId(id);
        if (!tacticsUserRepository.create(tacticsUser)){
            throw new BizException("创建战术学习失败");
        }

        Tactics tactics = tacticsRepository.getByIdCheck(tacticsUser.getTacticsId());
        if (tactics.getStudyNumber()==null) tactics.setStudyNumber(0);
        tactics.setStudyNumber(tactics.getStudyNumber() + 1);
        if (!tacticsRepository.edit(tactics)){
            throw new BizException("战术学习数量增加失败");
        }
    }

    /**
     * 获取战术 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTacticsListVo> getTacticsList(GetTacticsListParam param) {

        SelectTacticsDto dto = new SelectTacticsDto();
        BeanUtils.copyProperties(param, dto);
        Page<Tactics> page = tacticsRepository.selectTactics(dto);
        List<Tactics> tacticsList = page.getRecords();
        List<GetTacticsListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(tacticsList)){

            Map<Long, TacticsType> tacticsTypeMap=new HashMap<>();

            //提取出typeIds
            List<Long> ids = tacticsList.stream().filter(tactics -> tactics.getTacticsTypeId() != null).toList().stream().map(Tactics::getTacticsTypeId).distinct().collect(Collectors.toList());
            //根据ids获取type名称
            if (CollectionUtil.isNotEmpty(ids)){
                List<TacticsType> tacticsTypes = typeRepository.listByIds(ids);
                //根据id获取type名称
                if (CollectionUtil.isNotEmpty(tacticsTypes)){
                    tacticsTypeMap= tacticsTypes.stream().collect(Collectors.toMap(TacticsType::getId, v -> v));
                }
            }

            for (Tactics tactics : tacticsList) {
                GetTacticsListVo vo = new GetTacticsListVo();
                BeanUtils.copyProperties(tactics, vo);
                if (CollectionUtil.isNotEmpty(tacticsTypeMap)){
                    TacticsType tacticsType = tacticsTypeMap.get(tactics.getTacticsTypeId());
                    if (tacticsType!=null){
                        vo.setTacticsTypeName(tacticsType.getName());
                    }
                }
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取战术下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTacticsDownListVo> getTacticsDownList(GetTacticsDownListParam param) {
        Tactics po = new Tactics();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<Tactics> tacticsList = tacticsRepository.getListByEntity(po);

        List<GetTacticsDownListVo> list = new ArrayList<>();
        for (Tactics tactics : tacticsList) {
            GetTacticsDownListVo vo = new GetTacticsDownListVo();
            BeanUtils.copyProperties(tactics, vo);
            list.add(vo);
        }
        return list;
    }


}



